package com.app.juyanresturantapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.app.juyanresturantapp.bean.UsersBean;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity {
    TextView loginTelephone,loginPassord,register;
    Button loginBtn;
    String telephone,password;
    String TAG = "LoginActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();

        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this,RegistActivity.class);
                startActivity(intent);
            }
        });
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
                if (!(telephone.equals("")||password.equals(""))){
//        Log.e(TAG,telephone+"--"+password);
                    login();

                }else {
                    Toast.makeText(LoginActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }
    private void login() {

//创建Client
        OkHttpClient client = new OkHttpClient();
        RequestBody body = new FormBody.Builder()
                .add("telephone",telephone)
                .add("password",password)
                .build();
        //创建Request对象
        final Request request = new Request.Builder()
                .url(getResources().getString(R.string.url)+"/app11/user/login")
                .post(body)
                .build();

        final Call call = client.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG,"网络访问失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String data = response.body().string();
                Looper.prepare();
                Gson gson = new Gson();
                UsersBean usersBean =gson.fromJson(data,UsersBean.class);
                    if (usersBean.isSuccess()){
                        Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(LoginActivity.this,ProductListActivity.class);
                        startActivity(intent);
                    }else {
                        Toast.makeText(LoginActivity.this, "用户名或者密码错误", Toast.LENGTH_SHORT).show();
                    }
                Looper.loop();


            }
        });

    }
    private void initData() {
        telephone = loginTelephone.getText().toString();
        password = loginPassord.getText().toString();
    }

    private void initView() {
        loginTelephone = findViewById(R.id.loginTelephone);
        loginPassord = findViewById(R.id.loginPassord);
        register = findViewById(R.id.register);
        loginBtn = findViewById(R.id.loginBtn);
    }
}